define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/collocation/index' + location.search,
                    add_url: 'goods/collocation/add' + '?'+'good_id=' + Config.good_id,
                    // edit_url: 'goods/collocation/edit',
                    del_url: 'goods/collocation/del',
                    multi_url: 'goods/collocation/multi',
                    import_url: 'goods/collocation/import',
                    table: 'goods_collocation',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'goods_id', title: __('Goods_id')},
                        {field: 'child_goods_id', title: __('Child_goods_id')},
                        {field: 'goods_name', title: __('Goods_name'), operate:'BETWEEN'},
                        {field: 'preferential', title: __('Preferential'), operate:'BETWEEN'},
                        // {field: 'mark_price', title: __('Mark_price'), operate:'BETWEEN'},
                        // {field: 'commodity_price', title: __('Commodity_price'), operate:'BETWEEN'},
                        // {field: 'agency_price', title: __('Agency_price'), operate:'BETWEEN'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        select: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/controller/select',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                sortOrder:'asc',
                pagination: true,
                commonSearch: true,
                columns: [
                    [
                        /*  {checkbox: true},*/
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('名称'), operate: 'LIKE'},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    Fast.api.close(row);
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs">' +
                                    '<i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        selectgoods :function () {
// 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/collocation/selectgoods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                sortOrder:'asc',
                pagination: true,
                commonSearch: true,
                columns: [
                    [
                        /*  {checkbox: true},*/
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('商品名称'), operate: 'LIKE'},
                        {field: 'main_image', title: __('商品图片'), formatter: Table.api.formatter.images,operate:false},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    Fast.api.close(row);
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs">' +
                                    '<i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            Table.api.bindevent(table);
            $(document).on('click', '.img-center', function () {
                // window.open(this.src,'target','');
                // return false;
                var img = new Image();
                var imgWidth = this.getAttribute('data-width') || '800px';
                img.onload = function () {
                    var $content = $(img).appendTo('body').css({background: '#fff', width: imgWidth, height: 'auto'});
                    Layer.open({
                        type: 1, area: imgWidth, title: false, closeBtn: 1,
                        skin: 'layui-layer-nobg', shadeClose: true, content: $content,
                        end: function () {
                            $(img).remove();
                        },
                        success: function () {

                        }
                    });
                };
                img.onerror = function (e) {

                };
                img.src = this.src;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $("#id").change(function(){
                    console.log($(this).val(),'商品id');
                });

                var refreshkeygoods = function (data) {

                    var cimage = Fast.api.cdnurl(data.main_image);
                    var name   = Fast.api.cdnurl(data.name);
                    var item   = data.item;
                    console.log(item)
                    var option = '';
                    for(var i = 0; i < item.length; i++){
                        option += '<option value="'+item[i].id+'" name="" value="">'+item[i].spec_value_str +'</option>';
                    }

                    html = '<li class="col-xs-3">' +
                            '<a href="'+cimage+'" data-url="'+cimage+'" target="_blank" class="thumbnail">' +
                            '<img src="'+cimage+'" class="img-responsive"></a>' +
                            '<input type="text"   name="row[name]" disabled class="form-control"  value="'+name+'"/>' +
                            '<input type="hidden" name="row[id]" class="form-control" value="'+data.id+'"/>' +
                            '<a href="javascript:;" class="btn btn-danger btn-xs btn-trash"><i class="fa fa-trash"></i></a>' +

                            '<select class="form-control selectpicker" name="row[item]" id="test">' +
                                option +
                            '</select>'

                            '</li>';

                    $("body").find("li").remove();
                    $('#pre-goods').append(html);

                };

                // 移除按钮事件
                $(document.body).on("click", "#pre-goods .btn-trash", function () {
                    $(this).closest("li").remove();
                });
                // 新页面
                $(document).on('click','#select-goods', function (){
                    parent.Backend.api.open($(this).attr("href"), __('选择搭配商品'), {callback: refreshkeygoods});
                    return false;
                });
            }
        }
    };
    return Controller;
});
